package com.golxen.framework.netty.websocket;


import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

public class AgentWebSocketChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    public void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        //服务端判断读超时，判断 Server 是否在时间间隔内从 Channel 读取到数据
        pipeline.addLast("idleState", new IdleStateHandler(60, 0 ,0, TimeUnit.SECONDS));

        //GET请求
        pipeline.addLast(new HttpServerCodec());
        //POST请求
        pipeline.addLast(new HttpObjectAggregator(64 * 1024));

        pipeline.addLast(new AgentWebSocketHandler());

        //开放接口，握手、控制帧处理
        pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));
    }

}
